<template>
	<div class="loading-content" v-if="showloading">
		<div class="loading-spinner">
			<svg viewBox="25 25 50 50" class="circular">
				<circle cx="50" cy="50" r="20" fill="none" class="path"></circle>
			</svg>
		</div>
		<div class="loading-text">加载中...</div>
	</div>
</template>

<script>
	import Vue from 'vue'
	import * as dd from 'dingtalk-jsapi';
	export default {
		name: 'dingDing',
		data() {
			return {
				showloading: false,
				cropId: "",
				qxwx: false
			}

		},
		mounted() {

			var url = window.location.origin;
			Vue.prototype.$url = url + '/gd'
			sessionStorage.setItem('url', url + '/gd')

			var ua = window.navigator.userAgent.toLowerCase();
			// alert(ua.match(/MicroMessenger/i))
			if (ua.match(/MicroMessenger/i) == 'micromessenger') {
				this.qxwx = true
				sessionStorage.setItem('qxwxduan', false)
				this.getxwxcorpId()
			} else {
				this.qxwx = false
				sessionStorage.setItem('ding', false)
				this.getcorpId()
				// alert('不微信打开的')
			}


			this.showloading = true
		},
		activated() {
			this.showloading = false
		},
		methods: {

			getmobile() {
				console.log('getmobile')
				var that = this
				this.$http.get(this.$url + "/main/isMobile")
					.then(function(response) {

						setTimeout(function() {
							console.log(response.data.data)
						}, 100)
						if (response.data.data == 'true' || response.data.data == true) {
							console.log(window.location.origin + '/gdmobile/index.html' + window.location.search)
							// window.open(window.location.origin + '/gdmobile/index.html' + window.location.search)
						} else {
							// console.log(window.location.href)
							setTimeout(function() {
								console.log(window.location.href)
							}, 100)
							that.getcorpId()
						}
					})
					.catch(function(error) {
						// alert(error);
						console.log(error)
						that.$Toast('网络拥堵，刷新一下');
						// location.reload()
					});
			},
			getcorpId() {
				var that = this
				this.$http.get(this.$url + "/cropId")
					.then(function(response) {
						// console.log(response.data)
						that.cropId = response.data.data
						that.getcodes()
					})
					.catch(function(error) {
						// alert(error);
						console.log(error)
						that.$Toast('网络拥堵，刷新一下');
						// location.reload()
					});
			},
			getxwxcorpId() {
				// alert(this.$url + "/wecom/oauth2")
				var that = this
				this.$http.get(this.$url + "/wecom/oauth2")
					.then(function(response) {
						// alert(JSON.stringify(response.data))
						console.log('cookieid---' + response.headers.cookieid)
						sessionStorage.setItem('cookieid', response.headers.cookieid)

						sessionStorage.setItem('ding', true)
						if (response.data) {
							//1.后端已经绑定了钉钉，那么直接到首页
							// alert(response.headers.authorization)
							sessionStorage.setItem('token', response.headers.authorization)
							that.$http.defaults.headers["Authorization"] = sessionStorage.getItem('token');

							that.showloading = false

							that.getuid()
						} else {
							//2.没有绑定账号，跳转到登陆
							// that.$Toast.clear()
							that.showloading = false
							setTimeout(function() {
								that.$router.push("/login")
							}, 100)
						}
					})
					.catch(function(error) {
						// alert(JSON.stringify(error));
						console.log(error)
						// that.$Toast('网络拥堵，刷新一下');
						// location.reload()
					});
			},
			getcodes() {
				console.log('getcodes')
				var that = this
				dd.ready(function() {
					// alert('ready')
					// dd.ready参数为回调函数，在环境准备就绪时触发，jsapi的调用需要保证在该回调函数触发后调用，否则无效。
					dd.runtime.permission.requestAuthCode({
						corpId: that.cropId,
						onSuccess: function(result) {
							// alert(result)
							var code = result.code;
							//调用后端接口，获取信息
							sessionStorage.setItem('ding', true)
							console.log(code)
							that.getUserInfo(code);
							/*{
							    code: 'hYLK98jkf0m' //string authCode
							}*/
						},
						onFail: function(err) {
							// alert(err)
						}

					});
				});
			},
			getuid() {
				var that = this
				this.$http.get(this.$url + '/sys/user/detail')
					.then(function(response) {
						if (response.data.code == '1410') {
							that.$Toast(response.data.msg);
							sessionStorage.setItem('token', '')
							that.$router.push('/login')
							return
						}

						sessionStorage.setItem('uid', response.data.data.id)

						var urls = window.location.href
						console.log(window.location.href)
						var cs = urls.split('?')[1];
						// alert(cs)
						if (cs == 'a=1') {
							that.$router.push("/workorder")
						} else if (cs == 'a=1#/ding') {
							that.$router.push("/workorder")
						} else if (cs == 'a=2#/ding') {
							that.$router.push("/searchorder")
						} else if (cs == 'a=3#/ding') {
							that.$router.push("/day")
						} else {
							// setTimeout(function() {
							that.$router.push("/index")
							// }, 100)
						}
					})
					.catch(function(error) {
						console.log(error);
						that.$Toast('网络拥堵，刷新一下');
						// that.$router.push('/login')
					});
			},
			getUserInfo(code) {
				// alert(this.$url + '')
				var that = this
				this.$http.get(this.$url + "/dingtalk/login?code=" + code, {
						headers: {
							'cookieid': '',
							'Authorization': '',
						}
					})
					.then(function(response) {
						// alert(response.data)
						sessionStorage.setItem('dingding', response.data)
						sessionStorage.setItem('cookieid', response.headers.cookieid)

						sessionStorage.setItem('ding', true)

						if (response.data) {
							//1.后端已经绑定了钉钉，那么直接到首页
							sessionStorage.setItem('token', response.headers.authorization)
							that.$http.defaults.headers["Authorization"] = sessionStorage.getItem('token');

							that.showloading = false

							that.getuid()
						} else {
							//2.没有绑定账号，跳转到登陆
							// that.$Toast.clear()
							that.showloading = false
							setTimeout(function() {
								that.$router.push("/login")
							}, 100)
						}
					})
					.catch(function(error) {
						// alert(error);
						console.log(error)
						that.$Toast('网络拥堵，刷新一下');
						// location.reload()
					});


			},


		}
	}
</script>

<style scoped>
</style>
